# coding: utf-8

"""
    # @author:  zsj
    # @file:    models.py
    # @time:    2024/8/8 16:30
"""
from math import factorial

from sqlalchemy import table
from sqlalchemy.sql import func
from datetime import datetime
from src.entity.abase import TableFunc
from pydantic import BaseModel
from sqlmodel import SQLModel, Field, select, BigInteger, UniqueConstraint, String, union
from typing import Optional
from src.utils.tools import get_cur_time


class BasicModel(SQLModel):
    id: int | None = Field(primary_key=True, index=True, default=None)


class BasicCreateTime(SQLModel):
    create_time: Optional[str] = Field(default="", max_length=128, description="创建时间")

class BasicUpdateTime(SQLModel):
    update_time: Optional[str] = Field(default='', max_length=128, description="更新时间")

class BaseMenu(SQLModel, TableFunc):
    name: str = Field(max_length=128, description="名称", nullable=False)
    name_zh: str = Field(max_length=128, description="名称", nullable=False)
    type: str = Field(default="", description="类型",nullable=False)
    path: str = Field(default="", description="路径",nullable=False)
    icon: str = Field(default="", description="icon link")
    color: str = Field(default='', description="背景色")
    desc: str = Field(default="", max_length=128, description="描述")
    rank: int = Field(default=1, description="优先级")
    scale: float = Field(default=1, description="")
    margin: int = Field(default=5, description="")
    is_root: int = Field(default=1, description="根节点")
    child_node: str = Field(default='', description="子节点")
    state: int = Field(default=1, description="状态; 1: 启用; 0: 禁用")

class Menu(BaseMenu, BasicModel, BasicCreateTime, BasicUpdateTime, table=True):
    __tablename__ = "t_menu"

class CreateMenu(BaseMenu, BasicCreateTime):
    pass

class UpdateMenu(BaseMenu,BasicModel, BasicUpdateTime):
    pass

class BaseUser(SQLModel, TableFunc):
    name: Optional[str] = Field(default=None, max_length=128, description="", nullable=True)
    idcard: Optional[str] = Field(default=None, max_length=128, description="用户名", nullable=True)
    username: Optional[str] = Field(default=None, max_length=128, description="用户名", nullable=True)
    password: Optional[str] = Field(default=None, max_length=128, description="密码", nullable=True)
    email: Optional[str] = Field(default=None, max_length=128, description="邮箱")
    phone: Optional[str] = Field(default=None, max_length=128, description="联系方式")
    state: Optional[int] = Field(default=1, description="状态", nullable=True)

class User(BaseUser, BasicModel, BasicCreateTime, BasicUpdateTime, table=True):
    __tablename__ = "t_user"

class CreateUser(BaseUser, BasicCreateTime):
    pass

class UpdateUser(BaseUser, BasicModel, BasicUpdateTime):
    pass


class BaseApi(SQLModel, TableFunc):
    name: Optional[str] = Field(default='', max_length=128, description="名称", nullable=False)
    method: Optional[str] = Field(default='get', max_length=10, description="方法", nullable=False)
    path: Optional[str] = Field(default='', max_length=500, description="路径", nullable=False)
    content: Optional[str] = Field(default='', description="请求体")
    group: Optional[str] = Field(default='', max_length=50, description="分组", nullable=True)
    project: Optional[str] = Field(default='', max_length=128, description="项目", nullable=True)
    version: Optional[str] = Field(default='', max_length=10, description="版本", nullable=True)
    desc: Optional[str] = Field(default='', max_length=1000, description="描述", nullable=True)
    state: Optional[int] = Field(default=1, description="状态", nullable=True)


class Api(BaseApi, BasicModel, BasicCreateTime, BasicUpdateTime, table=True):
    __tablename__ = "t_api"

class CreateApi(BaseApi, BasicCreateTime):
    pass

class UpdateApi(BaseApi, BasicModel, BasicUpdateTime):
    pass


class BaseRole(SQLModel, TableFunc):
    user: Optional[str] = Field(default='', max_length=128, description="角色名称", nullable=True)
    state: Optional[int] = Field(default=1, description="状态; 1:启用; 0:禁用", nullable=False)
    desc: Optional[str] = Field(default='', max_length=10, description="描述", nullable=True)


class Role(BaseRole, BasicModel, BasicCreateTime, BasicUpdateTime, table=True):
    __tablename__ = "t_role"

class CreateRole(BaseRole, BasicCreateTime):
    pass

class UpdateRole(BaseRole, BasicModel, BasicUpdateTime):
    pass


class BaseGroup(SQLModel, TableFunc):
    name: Optional[str] = Field(default='', max_length=128, description="分组名称", nullable=True)
    state: Optional[int] = Field(default=1, description="状态; 1:启用; 0:禁用")
    desc: Optional[str] = Field(default='', max_length=10, description="分组描述", nullable=True)


class Group(BaseGroup,BasicModel, BasicCreateTime, BasicUpdateTime, table=True):
    __tablename__ = "t_group"

class CreateGroup(BaseGroup, BasicCreateTime):
    pass

class UpdateGroup(BaseGroup, BasicModel, BasicUpdateTime):
    pass


class BaseMindMap(SQLModel, TableFunc):
    title: str = Field(index=True, default="")
    content: str = Field(default="")
    format: str = Field(default="", max_length=50, description="描述", nullable=True)
    progress_s: str = Field(default="", max_length=50, description="冒烟", nullable=True)
    progress_t: str = Field(default="", max_length=50, description="测试", nullable=True)
    create_by: str = Field(default="", description="创建人", nullable=True)
    update_by: str = Field(default="", description="更新人", nullable=True)

class MindMap(BasicModel, BasicCreateTime, BasicUpdateTime, BaseMindMap, table=True):
    __tablename__ = "t_mindmap"
    is_deleted: int = Field(default=0, nullable=True)

class CreateMindMap(BaseMindMap, BasicCreateTime, BasicUpdateTime):
    pass

class UpdateMindMap(BaseMindMap, BasicModel, BasicUpdateTime):
    pass

class OutMindMap(BasicModel, BasicCreateTime, BasicUpdateTime, TableFunc):
    title: str = Field(index=True, default="")
    format: str = Field(default="", max_length=50, description="描述", nullable=True)
    progress_s: str = Field(default="", max_length=50, description="冒烟", nullable=True)
    progress_t: str = Field(default="", max_length=50, description="测试", nullable=True)
    create_by: str = Field(default="", description="创建人", nullable=True)
    update_by: str = Field(default="", description="更新人", nullable=True)
    is_deleted: int = Field(default=0, nullable=True)

class BaseMessage(SQLModel, TableFunc):
    user_id: str = Field(max_length=200, description="id")
    username: str = Field(max_length=200, description="用户名")
    content: str = Field(description="消息")
    msg_type: int = Field(description="0:chat; 1:mindmap; 2:text; 3:doc; ", default=0)
    editable: bool = Field(description="可编辑性", default=False)
    state: Optional[int] = Field(default=1, description="状态; 1:启用; 0:禁用")

class Message(BaseMessage, BasicModel, BasicCreateTime, BasicUpdateTime, table=True):
    __tablename__ = "t_message"
    pass

class CreateMessage(BaseMessage, BasicCreateTime):
    pass

class UpdateMessage(BaseMessage, BasicModel, BasicUpdateTime):
    pass

class BugBase(SQLModel, TableFunc):
    title: str = Field(index=True, default="")
    content1: str = Field(default="")
    product: str = Field(default="", max_length=50, description="产品线", nullable=True)
    discovery_env: str = Field(default="", max_length=50, description="发现环境", nullable=True)
    platform: str = Field(default="", max_length=50, description="平台", nullable=True)
    device_type: str = Field(default="", max_length=50, description="设备类型", nullable=True)
    state: Optional[int] = Field(default=1, description="状态;  0:待处理; 1: 已上线; 2:已解决; 3:已修复; 4:已关闭; 5:已忽略; 6:已拒绝; 7:已取消; 8:已过期; 9: 无法复现; 10:已转需求; 11: 已验证")
    content2: str = Field(default="", max_length=50, description="解决描述", nullable=True)
    start_time: Optional[datetime] = Field(default=None, description="开始时间")
    end_time: Optional[datetime] = Field(default=None, description="结束时间")
    developer: str = Field(default="", max_length=50, description="开发人", nullable=True)
    tester: str = Field(default="", max_length=50, description="测试人", nullable=True)
    link_req: str = Field(default="", max_length=50, description="关联需求", nullable=True)
    discovery_version: str = Field(default="", max_length=50, description="关联版本", nullable=True)
    rank: str = Field(default="", max_length=50, description="优先级", nullable=True)
    level: str = Field(default="", max_length=50, description="等级", nullable=True)
    update_by: str = Field(default="", description="更新人", nullable=True)
    operator: str = Field(default="", max_length=50, description="操作人", nullable=True)

class Bug(BugBase, BasicModel, BasicCreateTime, BasicUpdateTime, table=True):
    __tablename__ = "t_bug"
    
    
class CreateBug(BugBase, BasicCreateTime):
    pass

class UpdateBug(BugBase, BasicModel, BasicUpdateTime):
    pass
